﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using PlugNT.Custom;
using PlugNT.Cms.Core;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Create
{
    public partial class CreateHtml : AdminPage
    {
        private Templet curTemplet = new Templet();
        private Dictionary<string,int> newsCountTab = null;
        public string currpostfix = "";
        public string dicpath = "/";
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();
            

            currpostfix = (Request.QueryString["currpostfix"] == null) ? WebConfig.TablePostfix : Request.QueryString["currpostfix"].Trim();
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #region 数据绑定
        private void BindData()
        {
            //绑定文章数目
            DataTable tmpDt = curTemplet.GetCreateList(currpostfix);
            newsCountTab = new Dictionary<string, int>();
            foreach (DataRow dr in tmpDt.Rows)
            {
                newsCountTab.Add(dr["clsno"].ToString(), (int)dr["tcount"]);
            }
            //绑定要生成的栏目
            DataTable dt = curTemplet.GetBingList(currpostfix);
            this.rptMenuList.DataSource = dt.DefaultView;
            this.rptMenuList.DataBind();

            DataTable dt2 = new Tables().GetList();
            this.ddlTables.Items.Clear();
            foreach (DataRow dr in dt2.Rows)
            {
                string description = dr["description"].ToString().Trim();
                string postfix = dr["postfix"].ToString().Trim();
                ListItem li = new ListItem(description, postfix);
                if (currpostfix.ToString() == postfix)
                {
                    dicpath = dr["dicpath"].ToString();
                    dicpath = (dicpath == "") ? "/" : "/" + dicpath + "/";
                    li.Selected = true;
                }
                this.ddlTables.Items.Add(li);
            }
            this.ddlTables.Attributes.Add("onchange", "SubmitUrl();");

        }
        #endregion


        #region 显示数据处理
        protected void rptMenuList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal LitFirst = (Literal)e.Item.FindControl("LitFirst");
                Label LabClassNm = (Label)e.Item.FindControl("LabClassNm");

                Literal litNewsCount = (Literal)e.Item.FindControl("litNewsCount");
                Literal litExec = (Literal)e.Item.FindControl("litExec");
                Literal litFilePath = (Literal)e.Item.FindControl("litFilePath");
                HiddenField hidClsno = (HiddenField)e.Item.FindControl("hidClsno");

                string LitStyle = "<span style=\" width:{0}px; display:block; float:left; text-align:right; vertical-align:middle; line-height:25px; height:25px;\">{1}{2}</span>";
                string LitImg1 = "<img src=../images/openfolder.gif align=absmiddle />";
                string LitImg2 = "<img src=../images/folder.gif align=absmiddle />";
                string LitImg3 = "<img src=../images/t.gif align=absmiddle />";

                DataRowView drv = (DataRowView)e.Item.DataItem;
                int clsListLen = Convert.ToInt32(drv["clslistlen"]);
                if (clsListLen == 1)
                {
                    LabClassNm.Font.Bold = true;
                    LitFirst.Text = LitImg1;
                }
                else
                {
                    LitFirst.Text = string.Format(LitStyle, clsListLen * 20, LitImg3, LitImg2);
                }
                //显示模板路径
                string indexfile = drv["indexfile"].ToString().Trim();
                string listfile = drv["listfile"].ToString().Trim();
                string showfile = drv["showfile"].ToString().Trim();
                string clsname = drv["clsname"].ToString().Trim();
                litFilePath.Text = "<span title=\"" + GetTmpPath(indexfile, listfile, showfile) + "\">鼠标移上去显示</span>";
                //显示文章数目
                int tCount = 0;
                if (newsCountTab.TryGetValue(hidClsno.Value,out tCount))
                {
                    string str = "";
                    if (indexfile != "")
                    {
                        str = " <a href=\"#\" onclick=\"popitFrm('" + clsname + "聚合页','CreatingHtml.aspx?clsno=" + hidClsno.Value + "&type=index&currpostfix=" + currpostfix + "');\" >聚合</a>";
                    }
                    if (listfile != "")
                    {
                        if (str != "")
                        {
                            str += " | ";
                        }
                        str += " <a href=\"#\" onclick=\"popitFrm('" + clsname + "列表页','CreatingHtml.aspx?clsno=" + hidClsno.Value + "&type=list&currpostfix=" + currpostfix + "');\" >列表</a>";
                    }
                    if (showfile != "")
                    {

                        if (str != "")
                        {
                            str += " | ";
                        }
                        str += " <a href=\"#\" onclick=\"popitFrm('" + clsname + "内容页','CreatingHtml.aspx?clsno=" + hidClsno.Value + "&type=shows&currpostfix=" + currpostfix + "');\" >内容</a>";
                    }
                    if (tCount > 0)
                    {
                        str = (str != "") ? str : "<font style=\"color:red;\">(未绑定模板文件)</font>";
                    }
                    else
                    {
                        str = (str != "") ? ((indexfile != "" || listfile !="" ) ? str : "无需生成") : "<font style=\"color:red;\">(未绑定模板文件)</font>"; 
                    }

                    litExec.Text = str;
                    litNewsCount.Text = tCount.ToString();
                }
                else
                {
                    litExec.Text = "<font style=\"color:red;\">(绑定模板错误)</font>";
                    litNewsCount.Text =tCount.ToString();
                }
            }
        }
        #endregion



        public string GetTmpPath(string indexfile, string listfile, string showfile)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("聚集页模板：\n" + (indexfile == "" ? "" : indexfile.Replace(",", ",\n") + "\n"));
            sb.Append("列表页模板：\n" + (listfile == "" ? "" : listfile + "\n"));
            sb.Append("内容页模板：" + (showfile == "" ? "" : "\n" + showfile));
            return sb.ToString();
        }
    }
}

